﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace Reconciliation.Business
{
    public class Case
    {
        public static Domain.Case GetCase(int orderID)
        {
            try
            {
                return Data.Case.GetCase(orderID);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.CaseAuditorComment> GetCaseAuditorComment(int caseID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                List<Domain.CaseAuditorComment> caseAuditorCommentList = new List<Reconciliation.Domain.CaseAuditorComment>();
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.OrderListByInvoice.ToString());
                if (domainOrderList != null)
                {
                    Domain.Case domainCase = domainOrderList.Where(o => o.Case.CaseID.Equals(caseID)).Select(or => or.Case).FirstOrDefault();

                    if (domainCase != null)
                    {
                        caseAuditorCommentList = domainCase.CaseAuditorCommentList;
                    }
                }
                if (caseAuditorCommentList.Count == 0)
                {
                    caseAuditorCommentList = Data.CaseAuditorComment.GetCaseAuditorComment(caseID);
                }
                return caseAuditorCommentList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.CaseClientComment> GetCaseClientComment(int caseID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                List<Domain.CaseClientComment> caseClientCommentList = new List<Reconciliation.Domain.CaseClientComment>();
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.OrderListByInvoice.ToString());
                if (domainOrderList != null)
                {
                    Domain.Case domainCase = domainOrderList.Where(o => o.Case.CaseID.Equals(caseID))
                                            .Select(or => or.Case).FirstOrDefault();
                    if (domainCase != null)
                    {
                        caseClientCommentList = domainCase.CaseClientCommentList;
                    }

                }
                if (caseClientCommentList.Count == 0)
                {
                    caseClientCommentList = Data.CaseClientComment.GetCaseClientComment(caseID);
                }
                return caseClientCommentList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static List<Domain.CaseAttachment> GetCaseAttachment(int caseID)
        {
            try
            {
                List<Domain.Order> domainOrderList = null;
                List<Domain.CaseAttachment> caseAttachmentList = new List<Reconciliation.Domain.CaseAttachment>();
                domainOrderList = (List<Domain.Order>)BusinessCacheManager.GetData(BusinessCacheManager.Key.OrderListByInvoice.ToString());
                if (domainOrderList != null)
                {
                    Domain.Case domainCase = domainOrderList.Where(o => o.Case.CaseID.Equals(caseID))
                                            .Select(or => or.Case).FirstOrDefault();
                    if (domainCase != null)
                    {
                        caseAttachmentList = domainCase.CaseAttachmentList;
                    }               
                }
                //From DB if not found from Cache
                if (caseAttachmentList.Count == 0)
                {
                    caseAttachmentList = Data.CaseAttachment.GetCaseAttachment(caseID);
                }
                return caseAttachmentList;
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static void SetAttachment(Domain.CaseAttachment domainAttachment)
        {
            try
            {
                Data.CaseAttachment.SetAttachment(domainAttachment);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static void SetCaseClientComment(Domain.CaseClientComment domainCaseClientComment)
        {
            try
            {
                Data.CaseClientComment.SetCaseClientComment(domainCaseClientComment);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static void SetCaseAuditorComment(Domain.CaseAuditorComment domainCaseAuditorComment)
        {
            try
            {
                Data.CaseAuditorComment.SetCaseAuditorComment(domainCaseAuditorComment);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }

        public static Domain.Case UpdateCase(Domain.Case domainCase)
        {
            try
            {
                return Data.Case.UpdateCase(domainCase);
            }
            catch (Domain.ReconciliationException ex)
            {
                throw ex;
            }
        }
    }
}
